package cn.fancy.sdk.service;

import java.util.HashMap;
import java.util.Map;

import cn.fancy.sdk.service.utils.QdMakeLog;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class QdUserInfo {
	
	private static Context m_context = null;
	private static QdUserInfo instance = new QdUserInfo();
	private static String TAG = QdUserInfo.class.getSimpleName();
	private static Map<String, String> m_params = new HashMap<String, String>();
	
	final static String PARAMS_GAMEID		= "gameid";	
	final static String PARAMS_KEY				= "key";
	final static String PARAMS_GAMENAME = "gamename";
	final static String PARAMS_DEVICE 		= "device";
	final static String PARAMS_SYSTEMVER 	= "systemver";
	final static String PARAMS_IMEI 				= "imei";
	final static String PARAMS_MAC 			= "mac";
	final static String PARAMS_VERSION		= "sdkver";
	
	public final static String PARAMS_PLATFORMID		= "pid";
	public final static String PARAMS_DITCHID 			= "ditchid";
	public final static String PARAMS_SERVERID 			= "serverid";
	public final static String PARAMS_USERID 				= "uid";
	public final static String PARAMS_USER_NAME 		= "username";
	public final static String PARAMS_ROLEID 				= "roleid";
	public final static String PARAMS_ROLE_LEVEL 		= "level";
	public final static String PARAMS_CHARGE 			= "charge";
	public final static String PARAMS_VIP_LEVEL 			= "vip";
	
	static QdUserInfo setContext(Context context, String platformId, String ditchId)
	{
		QdMakeLog.d(TAG, "set userInfo");
		m_context = context;
		String gameid = "13";
		String gamekey = "EsL6uuXDEdSuyWAF";
		String gamename = "test";
		
		try {
			PackageManager packageManager = m_context.getApplicationContext().getPackageManager(); 
			ApplicationInfo appInfo = packageManager.getApplicationInfo(m_context.getPackageName(), PackageManager.GET_META_DATA);
			gamename = packageManager.getApplicationLabel(appInfo).toString();	
		} catch (Exception e) {
			e.printStackTrace();
			QdMakeLog.e(TAG, "Error:  meta-data configuration error");
		}
		
	    m_params.put(PARAMS_GAMEID, 			gameid);
	    m_params.put(PARAMS_KEY, 					gamekey);
	    m_params.put(PARAMS_GAMENAME, 		gamename);
	    m_params.put(PARAMS_DEVICE, 				getDevType());
	    m_params.put(PARAMS_SYSTEMVER, 		getSystemVersion());
	    m_params.put(PARAMS_IMEI, 					"");
	    m_params.put(PARAMS_MAC, 					getLocalMacAddress());
	    m_params.put(PARAMS_PLATFORMID, 	platformId);
	    m_params.put(PARAMS_DITCHID, 			ditchId);
	    m_params.put(PARAMS_VERSION, 			"300");
	    
		QdUserInfo.resetInfo();
		
		return instance;
	}
	
	/* put new pairs and return instance */
	static void setUserInfo(Map<String, String> params)
	{
		input(params, PARAMS_SERVERID);
		input(params, PARAMS_USERID);
		input(params, PARAMS_USER_NAME);
		input(params, PARAMS_ROLEID);
		input(params, PARAMS_ROLE_LEVEL);
		input(params, PARAMS_CHARGE);
		input(params, PARAMS_VIP_LEVEL);
	}
	
	static void setSingleParam(String key, String value)
	{
		if(key == null || value == null) {
			QdMakeLog.e(TAG, "Error: can not take in null strings");
			return;
		}
		m_params.put(key, value);
	}
	
	private static void input(Map<String, String> params, String key)
	{
		try {
			String _value = params.get(key).toString().trim();
			m_params.put(key, _value);
		} catch(Exception e) {
			if(key.equals(PARAMS_CHARGE) || key.equals(PARAMS_VIP_LEVEL)) return;
			QdMakeLog.e(TAG, "Error: QDWebusSdk never receive params of " + key + " correctly" );
		}
	}
	
	public static String output(String key)
	{
		return m_params.get(key).toString();
	}
	
	public static String output(Map<String, String> params, String key)
	{
		return output(params, key, null);
	}
	
	public static String output(Map<String, String> params, String srcKey, String  desKey)
	{
		if(!m_params.containsKey(srcKey) || m_params.get(srcKey)==null){
			return "";
		}
		String _value = m_params.get(srcKey);
		if(desKey == null || desKey.equals("")) {
			params.put(srcKey, _value);
		} else {
			params.put(desKey, _value);
		}
		return _value;
	}
	
	static void resetInfo() {
		m_params.put(PARAMS_SERVERID, "");
		m_params.put(PARAMS_USERID, "");
		m_params.put(PARAMS_USER_NAME, "");
		m_params.put(PARAMS_ROLEID, "");
		m_params.put(PARAMS_ROLE_LEVEL, "");
		m_params.put(PARAMS_CHARGE, "");
		m_params.put(PARAMS_VIP_LEVEL, "");
	}
	
	/*get device name */
	private static String getDevType() 
	{
		if (android.os.Build.MODEL == null || android.os.Build.MODEL.equals("")) return "default";
		return android.os.Build.MODEL;
	}
	
	/* get system version */
	private static String getSystemVersion() 
	{
		if (android.os.Build.VERSION.RELEASE == null
				|| android.os.Build.VERSION.RELEASE.equals("")) return "default";
		return android.os.Build.VERSION.RELEASE;
	}
	
	/* get mac address */
	private static String getLocalMacAddress() {
		return "default";
	}
	
}
